package top.kangyaocoding.ecommerce.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;
import top.kangyaocoding.ecommerce.IChatAIService;
import top.kangyaocoding.ecommerce.constants.Constants;
import top.kangyaocoding.ecommerce.dto.ChatAiDTO;
import top.kangyaocoding.ecommerce.exception.AppException;
import top.kangyaocoding.ecommerce.req.ChatAiReq;
import top.kangyaocoding.ecommerce.response.Response;
import top.kangyaocoding.ecommerce.utils.JwtUtil;

import java.util.List;

/**
 * 描述: AI 聊天控制器
 *
 * @author K·Herbert
 * @since 2025-04-04 22:21
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/chat/")
@CrossOrigin(origins = "*")
public class ChatAIController {

    private final IChatAIService chatAIService;
    private final JwtUtil jwtUtil;

    public ChatAIController(JwtUtil jwtUtil, IChatAIService chatAIService) {
        this.jwtUtil = jwtUtil;
        this.chatAIService = chatAIService;
    }

    @PostMapping(value = "findAll")
    public Response<List<ChatAiDTO>> findAll(@RequestHeader("Authorization") String token) {
        if (!jwtUtil.validateToken(token)) {
            log.error("Token 异常: {}", Constants.ErrorCode.TOKEN_ERROR.getInfo());
            return Response.<List<ChatAiDTO>>builder()
                    .code(Constants.ErrorCode.TOKEN_ERROR.getCode())
                    .info(Constants.ErrorCode.TOKEN_ERROR.getInfo())
                    .data(null)
                    .build();
        }
        return chatAIService.findAll(token);
    }

    @PostMapping(value = "upload/image", produces = MediaType.APPLICATION_JSON_VALUE)
    public Response<String> uploadImage(@RequestParam("file") MultipartFile file, @RequestHeader("Authorization") String token) {
        if (!jwtUtil.validateToken(token)) {
            log.error("Token 异常: {}", Constants.ErrorCode.TOKEN_ERROR.getInfo());
            return Response.<String>builder()
                    .code(Constants.ErrorCode.TOKEN_ERROR.getCode())
                    .info(Constants.ErrorCode.TOKEN_ERROR.getInfo())
                    .data(null)
                    .build();
        }
        return chatAIService.uploadImage(file, token);
    }

    @PostMapping(value = "stream/image", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<AssistantMessage> streamImage(@RequestBody ChatAiReq request, @RequestHeader("Authorization") String token) throws Exception {
        if (!jwtUtil.validateToken(token)) {
            log.error("Token 异常: {}", Constants.ErrorCode.TOKEN_ERROR.getInfo());
            return Flux.error(new AppException(
                    Constants.ErrorCode.TOKEN_ERROR.getCode(),
                    Constants.ErrorCode.TOKEN_ERROR.getInfo()
            ));
        }
        return chatAIService.streamImage(request, token);
    }
}


